import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import menuConfig from '../config/menu.json'

// 菜单项接口
interface MenuItem {
  path: string
  name: string
  title: string
  icon?: string
  component?: string
  children?: MenuItem[]
}

// 递归生成路由配置
function generateRoutes(menus: MenuItem[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []
  
  menus.forEach(menu => {
    if (menu.component) {
      routes.push({
        path: menu.path,
        name: menu.name,
        component: () => import(`../views/${menu.component}`)
      })
    }
    
    if (menu.children && menu.children.length > 0) {
      routes.push(...generateRoutes(menu.children))
    }
  })
  
  return routes
}

const routes = generateRoutes(menuConfig.menu)

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router 